package code.module.motion
{
	import code.app.AppCtrlList;
	import code.app.AppData;
	import code.app.AppView;
	
	import com.yy.manager.ResourceManager;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Point;

	/**
	 *常用动画控制器 表情动画除外 
	 * @author YY
	 * 
	 */
	public class MotionCtrl
	{
		private var _motionLayer:Sprite;
		private static var _instance:MotionCtrl;
		
		public static var winGameMotion:String = "ui.motion.WinGameMotion";
		
		public function MotionCtrl(xx:XX)
		{
			init();
		}
		
		private function init():void {
			_motionLayer = AppView.getInstance().motionLayer;
		}
		
		public static function getInstance():MotionCtrl {
			if(!_instance) {
				_instance = new MotionCtrl(new XX());
			}
			return _instance;
		}
		
		/**
		 *开始播放动画 
		 * @param key
		 * 
		 */
		public function startMotion(key:String, callBack:Function=null, pos:Point=null):void {
			var mc:MovieClip = ResourceManager.getMovieClipByName(key);
			if(mc) {
				_motionLayer.addChild(mc);
				if(Boolean(callBack)) {
					mc.callBack = callBack;
				}
				if(pos) {
					mc.x = pos.x;
					mc.y = pos.y;
				}else {
					mc.x = AppData.STAGE_WIDTH/2;
					mc.y = AppData.STAGE_HEIGHT/2;
				}
				mc.addEventListener(Event.ENTER_FRAME, enterFrameFunc);
				mc.gotoAndPlay(1);
			}else {
				trace("未找到动画资源");
			}
		}
		
		/**
		 * 
		 * @param e
		 * 
		 */
		private function enterFrameFunc(e:Event):void {
			var mc:MovieClip = MovieClip(e.currentTarget);
			if(mc.currentFrame == mc.totalFrames) {
				mc.removeEventListener(Event.ENTER_FRAME, enterFrameFunc);
				if(_motionLayer.contains(mc)) {
					_motionLayer.removeChild(mc);
				}
				if(Boolean(mc.callBack)) {
					mc.callBack();
					mc.callBack = null;
				}
			}
		}
	}
}

class XX{}